import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class frame {

	static String ans1 = null;
	static String ans2 = null; //initializes all this stuff
	static String ans3 = null;
	static String ans4 = null;
	static String ans5 = null;

	static double Ans1;
	static double Ans2;
	static double Ans3;
	static double Ans4;
	static double Ans5;

	static boolean error = true; // false means an error occurred

	public static void inputFrame(String title, String msg0, String msg1, //the method
			String msg2, String msg3, String msg4, String msg5, int blanks) {
		

		JTextField box1 = new JTextField(10);
		JTextField box2 = new JTextField(10); //makes all the text input fields
		JTextField box3 = new JTextField(10);
		JTextField box4 = new JTextField(10);
		JTextField box5 = new JTextField(10);

		JPanel myPanel = new JPanel(); //creates the JPanel

		myPanel.add(new JLabel(msg0)); // adds the first 2 messages
		myPanel.add(new JLabel(msg1));
		myPanel.add(box1);

		if (msg2 != null) {
			myPanel.add(new JLabel(msg2)); //if statements to see if the following messages are valid
			myPanel.add(box2);
		}
		if (msg3 != null) {
			myPanel.add(new JLabel(msg3));
			myPanel.add(box3);
		}
		if (msg4 != null) {
			myPanel.add(new JLabel(msg4));
			myPanel.add(box4);
		}
		if (msg5 != null) {
			myPanel.add(new JLabel(msg5));
			myPanel.add(box5);
		}

		myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS)); //makes the frame nice and neat

		JOptionPane.showOptionDialog(null, myPanel, title, //displays it
				JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null,
				new String[] { "Calculate!" }, null);
		
		int blank = 0; //used for error testing
		
		if (msg1 != null) { //gets the text and parses it
			ans1 = box1.getText();
			if (errorCheck(ans1) == false && ans1.length() != 0) {
				errorMessage("You entered a non-numerical value.");
				error = true;
				return;
			} else if (errorCheck(ans1) == true)
				Ans1 = Double.parseDouble(ans1);
			if (ans1.length() == 0) blank++;
		}
		if (msg2 != null) {
			ans2 = box2.getText();
			if (errorCheck(ans2) == false && ans2.length() != 0) {
				errorMessage("You entered a non-numerical value.");
				error = true;
				return;
			} else if (errorCheck(ans2) == true)
				Ans2 = Double.parseDouble(ans2);
			if (ans2.length() == 0) blank++;
		}
		if (msg3 != null) {
			ans3 = box3.getText();
			if (errorCheck(ans3) == false && ans3.length() != 0) {
				errorMessage("You entered a non-numerical value.");
				error = true;
				return;
			} else if (errorCheck(ans3) == true)
				Ans3 = Double.parseDouble(ans3);
			if (ans3.length() == 0) blank++;
		}
		if (msg4 != null) {
			ans4 = box4.getText();
			if (errorCheck(ans4) == false && ans4.length() != 0) {
				errorMessage("You entered a non-numerical value.");
				error = true;
				return;
			} else if (errorCheck(ans4) == true)
				Ans4 = Double.parseDouble(ans4);
			if (ans4.length() == 0) blank++;
		}
		if (msg5 != null) {
			ans5 = box5.getText();
			if (errorCheck(ans5) == false && ans5.length() != 0) {
				errorMessage("You entered a non-numerical value.");
				error = true;
				return;
			} else if (errorCheck(ans5) == true)
				Ans5 = Double.parseDouble(ans5);
			if (ans5.length() == 0) blank++;
		}
		if (blank != blanks) { //checks to see if the correct amount of blanks are used.
			blankError(blanks);
			error = false;
			return;
		}
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////
	// /////////////////////////////////////////////////////////////////////////////////////////////////

	static void errorMessage(String string) { //displays the error message
		JOptionPane.showOptionDialog(null,
				string, "Error Code 1D10T",
				JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null,
				new String[] { "Sorry" }, null);
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////
	// /////////////////////////////////////////////////////////////////////////////////////////////////

	public static boolean errorCheck(String str) { //checks for errors when parsing
		if (str == "ans1")
			str = ans1;
		if (str == "ans2")
			str = ans2;
		if (str == "ans3")
			str = ans3;
		if (str == "ans4")
			str = ans4;
		if (str == "ans5")
			str = ans5;

		try {
			Double.parseDouble(str);
		} catch (NumberFormatException e) {
			return false;
		}

		return true;
	}

	public static void blankError(int a) { //displays the error message for blank fields
		String spaces = "space";
		if (a != 1)
			spaces = "spaces";
		else if (a == 1)
			spaces = "space";
		JOptionPane.showOptionDialog(null, "You need to leave exactly " + a
				+ " " + spaces + " blank.", "Error Code 1D10T",
				JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null,
				new String[] { "Sorry" }, null);
	}
	
	public static int randInt(int min, int max) {
		Random rand = new Random();
		return rand.nextInt(max + 1 - min) + min;
	}

}